import { ref } from 'vue'
import app from '@/app'
import store from '@/app/store'
import { get, update, del } from '@/api/store/goods_menu'

// 初始化
const menuStore = store()

// 焦点
menuStore.index = ref('all')

menuStore.get = () => {
  return new Promise((resolve, jeject) => {
    get().then(res => {
      menuStore.setList(res)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

menuStore.update = (...arge) => {
  return new Promise((resolve, jeject) => {
    update(...arge).then(res => {
      menuStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

menuStore.del = (...arge) => {
  return new Promise((resolve, jeject) => {
    del(...arge).then(res => {
      menuStore.index.value = 'all'
      menuStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

menuStore.getIndex = () => {
  return menuStore.index.value == 'all' ? null : menuStore.index.value
}

menuStore.getIndexData = () => {
  return menuStore.getList().find(item => item.id == menuStore.getIndex())
}

// 搜索id
menuStore.id = id => {
  return menuStore.getList().find(item => item.id == id)
}

menuStore.name = name => {
  return menuStore.getList().find(item => item.name == name)
}


export default menuStore